我們會用到均方根誤差,均方根偏差 root mean squared error
常用於衡量模型預測值或估計量與觀測值之間差異的一種指標。
def root_mean_squared_error(y_true, y_pred):
return np.sqrt(np.mean(np.square(y_pred - y_true)))
def multivariate_data(dataset, target, start_index, end_index, history_size, target_size, single_step=False):
data = []
labels = []
start_index = start_index + history_size
if end_index is None:
end_index = len(dataset) - target_size
for i in range(start_index, end_index):
indices = range(i-history_size, i)
data.append(dataset[indices])
if single_step:
labels.append(target[i+target_size])
else:
labels.append(target[i:i+target_size])
return np.array(data), np.array(labels)
y = data_input.close_price
x = data_input
def build_model(input_length, input_dim):
d=0.3
model= Sequential()
model.add(LSTM(128,input_shape=(input_length, input_dim),return_sequences=True))
model.add(Dropout(d))
model.add(LSTM(64,input_shape=(input_length, input_dim),return_sequences=False))
model.add(Dropout(d))
model.add(Dense(1,activation='linear'))
#linear / softmax(多分類) / sigmoid(二分法)
model.compile(loss='mse',optimizer='adam')
return mode1